/**
 * MyToken.java 1.0 2007-6-1 下午07:57:55,创建 by yangfan
 */


package org.in4j.search.query.parser;

import java.util.HashSet;
import java.util.Set;
import org.apache.commons.lang.builder.ToStringBuilder;

/**
 *
 * @author yangfan
 * @version 1.0 2007-6-1 下午07:57:55,创建
 */
public class MyToken
{

	private String value;

	public MyToken(String value)
	{
		this.value = value;
	}

	public boolean isOperator()
	{
		return OPERATORS.contains(value.toLowerCase());
	}

	public boolean isRelation()
	{
		return RELATIONS.contains(value.toLowerCase());
	}

	public boolean isVar()
	{
		return !(isOperator() || isBracket() || isRelation());
	}

	public boolean isPreBracket()
	{
		return "(".equals(value);
	}

	public boolean isPostBracket()
	{
		return ")".equals(value);
	}

	public String getValue()
	{
		return value;
	}

	public int getLevel()
	{
		if(isBracket())
			return 3;
		if(isOperator())
			return 2;
		if(isRelation())
			return 1;
		return 0;
	}

	public boolean isBracket()
	{
		return isPreBracket() || isPostBracket();
	}

	private static final Set<String> RELATIONS = createRelations();

	private static final Set<String> OPERATORS = createOperators();


	private static Set<String> createRelations()
	{
		Set<String> relations = new HashSet<String>();
		relations.add("and");
		relations.add("or");
		return relations;
	}

	private static Set<String> createOperators()
	{
		Set<String> operators = new HashSet<String>();
		operators.add("=");
		operators.add("like");
		return operators;
	}

	@Override
	public String toString()
	{
		StringBuffer sb = new StringBuffer();
		if(isOperator())
			sb.append("[OPERATOR]");
		if(isRelation())
			sb.append("[RELATION]");
		if(isBracket())
			sb.append("[BRACKET]");
		sb.append(value);
		return sb.toString();
	}
}
